﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;

namespace XUHttpDownload
{
    namespace Tests
    {
        public class XUHttpSocketTests1 : MonoBehaviour
        {
            XUHttpSocket httpSocket = null;

            Dictionary<string, bool> urls = new Dictionary<string, bool>()
            {
                { "http://swbtcdn.hnputihd.com/data/subpver/a/3/2/13/ava_m_B010.ab", false},
                { "http://swbtcdn.hnputihd.com/data/subpver/a/3/2/13/ava_m_B011.ab", false},
                { "http://swbtcdn.hnputihd.com/data/subpver/a/3/2/13/ava_m_B012.ab", false},
                { "http://swbtcdn.hnputihd.com/data/subpver/a/3/2/13/ava_m_B013.ab", false},
                { "http://swbtcdn.hnputihd.com/data/subpver/a/3/2/13/ava_m_B014.ab", false},
                { "http://swbtcdn.hnputihd.com/data/subpver/a/3/2/13/ava_m_B015.ab", false},
            };

            void Start()
            {
                httpSocket = new XUHttpSocket("swbtcdn.hnputihd.com", 80, 1024 * 50, 1024 * 1024 * 20);
            }

            void OnDestroy()
            {
                httpSocket.Close();
            }

            void OnGUI()
            {
                foreach (var url in urls)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.TextField(url.Key, GUILayout.Width(500));
                    GUILayout.Toggle(url.Value, "完成");
                    GUILayout.EndHorizontal();
                }

                if (GUILayout.Button("下载"))
                {
                    Download();
                }

                if (GUILayout.Button("取消"))
                {
                    httpSocket.CancelDownload();
                }
            }

            void Download()
            {
                foreach (var url in urls)
                {
                    if (url.Value == false)
                    {
                        StartCoroutine(httpSocket.RequestDownload(url.Key, OnSuccess, OnFail, OnProgress));
                        break;
                    }
                }
            }

            void OnSuccess(string url, object param, int fileLength, byte[] fileBuff)
            {
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.Write(fileBuff, 0, fileLength);
                AssetBundle assetBundle = AssetBundle.LoadFromStream(memoryStream);
                Object[] objects = assetBundle.LoadAllAssets();
                foreach (var obj in objects)
                {
                    if (obj is GameObject)
                    {
                        Instantiate(obj);
                    }
                }

                urls[url] = true;

                Download();
            }

            void OnFail(string url, object param)
            {
                urls[url] = false;
            }

            void OnProgress(string url, object param, float progress)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("download progress = " + progress + " url = " + url);
                Debug.Log(stringBuilder.ToString());
            }
        }
    }
}